package Demo78;

import java.util.PriorityQueue;

/**
 * 最后一块石头重量
 * https://leetcode.cn/problems/last-stone-weight/
 */
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
        for (int num : stones) {
            queue.add(num);
        }
        while(queue.size() >= 2) {
            int x = queue.poll();
            int y = queue.poll();
            if(x != y) queue.add(Math.abs(x - y));
        }
        return !queue.isEmpty() ? queue.poll() : 0;
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int result = solution.lastStoneWeight(new int[]{2,7,4,1,8,1});
        System.out.println(result);
    }
}